package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"

	"gitee.com/yangangao/gadgets/wbSpider/excel"
)

func WBHttpGet(url string) (reslut string, err error) {
	client := &http.Client{}

	//提交请求
	reqest, err := http.NewRequest("GET", url, nil)
	
	if err != nil {
		panic(err)
	}
	//处理返回结果
	resp, _err := client.Do(reqest)
	if _err != nil {
		err = _err
	}
	defer resp.Body.Close()

	content, _err2 := ioutil.ReadAll(resp.Body)
	if _err2 != nil {
		err = _err2
	}
	reslut = string(content)
	return
}

// there is two kind of weibo text 
func getWeiboLongText(id, isLongText, created_at []string, filename, username, keyword string, countbase int) {
	if debug {
		Debug.Println("getWeiboLongText", isLongText)
	}
	url := `https://weibo.com/ajax/statuses/longtext?id=`
	var re, idurl string
	var result [][]string
	var err error

	retry := make([]string, 0, 5)

	// var r *regexp.Regexp
	r := regexp.MustCompile(`longTextContent":"(.*?)"`)

	for index, item := range id {
		//Debug.Printf("%T,%v", isLongText[index], isLongText[index])
		if isLongText[index] != "true" {
			if debug {
				Debug.Println("检测到非长文本项，跳过", index, index+countbase)
			}
			continue
		}
		if debug {
			Debug.Println("检测到长文本项，执行", index, index+countbase)
		}

		idurl = url + item
		re, err = WBHttpGet(idurl)
		if err != nil {
			fmt.Println(err.Error())
		}

		result = r.FindAllStringSubmatch(re, -1)
		if len(result) == 0 {
			retry = append(retry, item)
			if debug {
				Debug.Println(item, "未获取到数据, 已将其添加到重试列表:", retry)
			}
			time.Sleep(500 * time.Millisecond)
			continue
		}
		Info.Println("正在保存长文本，序号：", index, index+countbase, idurl)
		excel.AddContentByRow(filename, []string{strconv.Itoa(index + countbase), "是", keyword, username, created_at[index], result[0][1]})
		time.Sleep(500 * time.Millisecond)
	}

	if len(retry) != 0 {
		if debug {
			Debug.Println("重新尝试获取:", retry)
		}

		for _, item := range retry {
			idurl = url + item
			re, err = WBHttpGet(idurl)
			if err != nil {
				fmt.Println(err.Error())
			}

			result = r.FindAllStringSubmatch(re, -1)
			if len(result) == 0 {
				if debug {
					Debug.Println(item, "仍旧没有得到数据，我们放弃并使用短数据代替:", re)
				}

				for index, i := range id {
					if i == item {
						isLongText[index] = "long2short"
						break
					}
				}
				time.Sleep(1 * time.Second)
				continue
			}
			if debug {
				Info.Println("正在重新请求长文本，序号：", idurl)
			}

			for index, i := range id {
				if i == item {
					excel.AddContentByRow(filename, []string{strconv.Itoa(index), "是", keyword, username, created_at[index], result[0][1]})
					break
				}
			}
			time.Sleep(1 * time.Second)
		}
	}
}

func getRawText(rawtext, isLongText, created_at []string, filename, username, keyword string, countbase int) {
	if debug {
		Debug.Println("getRawText", isLongText)
	}

	for index, item := range rawtext {
		switch isLongText[index] {
		case "true":
			if debug {
				Debug.Println("执行true, 条件是", isLongText[index])
			}

			continue
		case "false":
			if debug {
				Debug.Println("执行false, 条件是", isLongText[index])
			}

			Info.Println("正在保存文本，序号：", index)
			excel.AddContentByRow(filename, []string{strconv.Itoa(index + countbase), "否", keyword, username, created_at[index], item})
		case "long2short":
			if debug {
				Debug.Println("执行long2short, 条件是", isLongText[index])
			}

			Info.Println("正在保存长转短文本，序号：", index)
			excel.AddContentByRow(filename, []string{strconv.Itoa(index + countbase), "长转短", keyword, username, created_at[index], item})
		}
	}
}

func rawMsgHandler(rawmsg string, r ...*regexp.Regexp) (mblogid, isLongText, text_raw, created_at []string, length int) {
	re_mblogid := r[0].FindAllStringSubmatch(rawmsg, -1)
	re_isLongText := r[1].FindAllStringSubmatch(rawmsg, -1)
	re_text_raw := r[2].FindAllStringSubmatch(rawmsg, -1)
	re_created_at := r[3].FindAllStringSubmatch(rawmsg, -1)

	for _, item := range re_mblogid {
		mblogid = append(mblogid, item[1])
	}
	for _, item := range re_isLongText {
		isLongText = append(isLongText, item[1])
	}
	for _, item := range re_text_raw {
		text_raw = append(text_raw, item[1])
	}
	for _, item := range re_created_at {
		context := strings.Fields(item[1])
		if len(context) == 6 {
			// Debug.Println(context) //[Wed May 25 09:40:01 +0800 2022]
			created_at = append(created_at, context[1]+"/"+context[2]+"/"+context[5]+" "+context[3])
			// } else if len(context) == 1 {
			// 	// var tmp string
			// 	// for _, v := range context {
			// 	// 	tmp += v
			// 	// }
			// created_at = append(created_at, context[0])
		} else {
			// Debug.Println(context) //[2022-09-20]
			fmt.Println("time is error:", len(context), context)
		}
	}
	//Info.Println(len(text_raw), text_raw[:1][:20])
	//Info.Println(len(mblogid), mblogid)
	if debug {
		Debug.Println(len(isLongText), isLongText)
	}

	text_raw_len := len(text_raw)
	mblogid_len := len(mblogid)
	isLongText_len := len(isLongText)
	created_at_len := len(created_at)

	if !(text_raw_len == mblogid_len &&
		mblogid_len == isLongText_len &&
		isLongText_len == created_at_len) {
		// if text_raw_len != mblogid_len ||
		// 	text_raw_len != isLongText_len ||
		// 	mblogid_len != isLongText_len ||
		// 	created_at_len != text_raw_len {
		if debug {
			switch {
			case text_raw_len != mblogid_len:
				Debug.Println("text_raw_len!=mblogid_len", text_raw_len, mblogid_len)
			case text_raw_len != isLongText_len:
				Debug.Println("text_raw_len!=isLongText_len", text_raw_len, isLongText_len)
			case text_raw_len != created_at_len:
				Debug.Println("text_raw_len!=created_at_len", text_raw_len, created_at_len)
			case mblogid_len != isLongText_len:
				Debug.Println("mblogid_len!=isLongText_len", mblogid_len, isLongText_len)
			case mblogid_len != created_at_len:
				Debug.Println("mblogid_len!=created_at_len", mblogid_len, created_at_len)
			case isLongText_len != created_at_len:
				Debug.Println("isLongText_len!=created_at_len", isLongText_len, created_at_len)
			}
			Debug.Println("len does not match:", text_raw_len, mblogid_len, isLongText_len, text_raw_len, created_at_len)
			Debug.Println(text_raw, "\n",
				mblogid, "\n",
				isLongText, "\n",
				// text_raw, "\n",
				created_at)
		}
		panic("len does not match")
	}

	length = text_raw_len
	return

}
